<?php

class StdLibBinaryOperators implements CliLibrary
{
	private static $operators = array(
		ExpOperator::TYPE_PREFIX => array(
			'~' => array(PREC_PREFIX, 'op_complement'),
		),
		ExpOperator::TYPE_LEFT => array(
			'<<' => array(PREC_SHIFT, 'op_leftshift'),
			'>>' => array(PREC_SHIFT, 'op_rightshift'),
			'&' => array(PREC_BINARYAND, 'op_bit_and'),
			'band' => array(PREC_BINARYAND, 'op_bit_and'),
			'bxor' => array(PREC_BINARYXOR, 'op_bit_xor'),
			'bor' => array(PREC_BINARYOR, 'op_bit_or'),
			'|' => array(PREC_BINARYOR, 'op_bit_or'),
		),
	);

	public static function addLibrary (CliMode $mode)
	{
		$mode->addOperators(__CLASS__, self::$operators);
	}

	public static function op_complement (ArgContext $args)
	{
		$args->operandCount(1);
		return ~$args->toNumber(0);
	}

	public static function op_leftshift (ArgContext $args)
	{
		$args->operandCount(2);
		return $args->toNumber(0) << $args->toNumber(1);
	}

	public static function op_rightshift (ArgContext $args)
	{
		$args->operandCount(2);
		return $args->toNumber(0) >> $args->toNumber(1);
	}

	public static function op_bit_and (ArgContext $args)
	{
		$args->operandCount(2);
		return $args->toNumber(0) & $args->toNumber(1);
	}

	public static function op_bit_xor (ArgContext $args)
	{
		$args->operandCount(2);
		return $args->toNumber(0) ^ $args->toNumber(1);
	}

	public static function op_bit_or (ArgContext $args)
	{
		$args->operandCount(2);
		return $args->toNumber(0) | $args->toNumber(1);
	}
}